package com.accp.crm.service;

import java.util.List;

import org.hibernate.SessionFactory;

import com.accp.crm.dao.CommonDAO;
import com.accp.crm.entity.BasDict;
import com.accp.crm.entity.PageBean;

public class BasdictService implements IBasdictService {
	
	public CommonDAO<BasDict> basdao;
	
	public void setSessionFactory(SessionFactory sessionFactory){
		basdao = new CommonDAO<BasDict>(sessionFactory,BasDict.class);
	}


	@Override
	public List findByKhHql(String kh) {

		String hql = "from BasDict b where b.dictType=?";
		
		return this.basdao.findByHQL(hql, kh);
	}


	@Override
	public List findByDqHql(String dq) {
		
		String hql = "from BasDict b where b.dictType=?";
		
		return this.basdao.findByHQL(hql, dq);
	}


	@Override
	public List findByZtHql(String status) {
        String hql = "from BasDict b where b.dictType=?";	
		return this.basdao.findByHQL(hql,status);
	}
	
	public PageBean findAll(int pageNo,String type,String item,String value) {
		String hql="from BasDict b where 1=1";
		if (type!=null){
			hql += "and b.dictType like '%"
					+type+"%' ";
		}
		if (item!=null){
			hql += "and b.dictItem like '%"
				+item+"%' ";
		}
		if (value!=null){
			hql += "and b.dictValue like '%"
				+value+"%' ";
		}
		return this.basdao.findByPage(hql,pageNo, 10);
	}


	@Override
	public void add(BasDict bas) {
		this.basdao.save(bas);
	}
	
	public void delete(Long id){
		BasDict bas=this.basdao.findById(id);
		this.basdao.delete(bas);
	}
	
	public void update(BasDict bas){
		this.basdao.update(bas);
	}
	
	public BasDict findById(Long id){
		return this.basdao.findById(id);
	}


	@Override
	public BasDict findByHql(String level) {
		String hql="from BasDict b where b.dictValue=?";
		List list=this.basdao.findByHQL(hql,level);
		return (BasDict) list.get(0);
	}

}
